import React from "react";
import styles from './index.less';
import { Modal, DatePicker, InputItem, Switch, Toast } from 'antd-mobile';
import ReactDOM from 'react-dom';
import SeModal from '@/components/searchEmployee';
import { getMastId } from '@/utils/tools';
import UserServices from '@/services/user';
import { requestSuccess, addHidden, removeHidden } from '@/utils/tools';

// 请求后返回值
class addAccommodationInfoModal extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      visible: true,
      message: props.message,
      content: props.content,
      success: props.success,
      cancel: props.cancel,
      datepickerActiveKey: '',
      datepickerVisible: false,
      orderNo: '',
      values: {
        traveller: '',
        city: '',
        hotelName: '',
        nightCount: '',
        checkInDate: '',
        checkOutDate: '',
        price: '',
        exceedStd: '',
        confirmFlag: '',
        describe: '',
        remark: ''
      }
    };
    window.cordovaBackEvents.push(() => {
      this.handleCancel();
    });
  }

  handleShowDatePicker = (activeKey) => {
    this.setState({
      datepickerActiveKey: activeKey,
      datepickerVisible: true
    })
  }

  handleFormatData = (data) => {
    const id = getMastId();

    return {
      ...id,
      accommodationInfo:{
        ...data,
        exceedStd: data.exceedStd ? 'YES':'NO',
        confirmFlag: data.confirmFlag ? 'YES':'NO'
      }
    }
  }

  handleSuccess = (e) => {
    const data = this.handleFormatData(this.state.values);

    const errorFn = (e) => {
      if(e){
        const message = e.message || 'Error';
        Toast.fail(message);
      }
    }
    UserServices.saveAccommodationInfo(data).then(res => {
      if(requestSuccess(res)){
        this.setState({
          visible: false,
        });
        removeHidden();
        this.state.success && this.state.success(res.data.mobileAccommodationInfo);
      }else{
        const errorInfo = global._.get(res, 'data.errorMessageList');
        errorFn(new Error(errorInfo))
      }
    }).then(errorFn)
    // this.setState({
    //   visible: false,
    // });
    // this.state.success && this.state.success({...this.state.values});
  }

  handleCancel = (e) => {
    removeHidden();
    this.setState({
      visible: false,
    });
    this.state.cancel && this.state.cancel();
  }

  handleSaveDatepickerValue = (date) => {
    if(this.state.datepickerActiveKey){
      this.onChange(global.moment(date).format('YYYY-MM-DD').valueOf(), this.state.datepickerActiveKey);
      this.setState({
        datepickerVisible: false
      })
    }
  }

  onChange = (value, key) => {
    this.setState({
      values: {
        ...this.state.values,
        [key]: value
      }
    })
  }

  handleSelectedEmployee = (key)=>{
    SeModal.open({
      success: (result) => {
        this.setState({values: {
          ...this.state.values,
          [key]: result.employees
        }})
      },
      cancel: () => {

      }
    })
  }

  handleSearch = () => {
    const id = getMastId(); //{traMastId: xx}

    const errorFn = (e) => {
      console.log(e);
      if(e){
        const message = e.message || 'Error';
        Toast.fail(message);
      }
    }
    UserServices.getAccommodationInfoByCtrip({
      ...id,
      accommodationInfo: {
        orderNo: this.state.orderNo
      }
    }).then(res => {
      if(requestSuccess(res)){
        console.log(res);
        this.setState({
          visible: false,
        });
        this.state.success && this.state.success('fromCtripSuccess');
      }else{
        const errorInfo = global._.get(res, 'data.errorMessageList');
        errorFn(new Error(errorInfo))
      }
    }).then(errorFn).catch(errorFn)
  }

  componentDidMount(){
    addHidden();
  }

  render() {
    return (
      <Modal
        visible={this.state.visible}
        className={styles.addAccommodationInfoModal}
      >
      <div className={styles.modalWrapper}>
        <DatePicker
          visible={this.state.datepickerVisible}
          mode='date'
          value={this.state.datepickerValue}
          onOk={this.handleSaveDatepickerValue}
          onDismiss={() => this.setState({ datepickerVisible: false })}
        />
      <div className={styles.info}>Accommodation Info</div>
        <div className={styles.results}>
          <div className={styles.list}>
            <div className={styles.header}>Ctrip Order</div>
            <div className={styles.item}>
              <div className={styles.des}>*Ctrip Order No</div>
              <div className={styles.value}>
                <InputItem
                  clear
                  placeholder="click input"
                  value={this.state.orderNo}
                  onChange={(value) => this.setState({orderNo: value})}
                  ></InputItem>
              </div>
            </div>
            <div className={styles.searchBtn} onClick={this.handleSearch}>Get Detail From Ctrip</div>
          </div>
          <div className={styles.list}>
          <div className={styles.header}>Non-Ctrip Order</div>
            <div className={styles.item} onClick={() => this.handleSelectedEmployee('traveller')}>
              <div className={styles.des}>*Traveller</div>
              <div className={styles.value}>{this.state.values.traveller || 'Select'}</div>
            </div>
          <div className={styles.item}>
            <div className={styles.des}>*City</div>
            <div className={styles.value}>
              <InputItem
                clear
                placeholder="click input"
                value={this.state.values.city}
                onChange={(value) => this.onChange(value, 'city')}
              ></InputItem>
            </div>
          </div>
          <div className={styles.item}>
            <div className={styles.des}>*Hotel</div>
            <div className={styles.value}>
              <InputItem
                clear
                placeholder="click input"
                value={this.state.values.hotelName}
                onChange={(value) => this.onChange(value, 'hotelName')}
              ></InputItem>
            </div>
          </div>
          <div className={styles.item}>
            <div className={styles.des}>*Night(s)</div>
            <div className={styles.value}>
              <InputItem
                clear
                placeholder="click input"
                value={this.state.values.nightCount}
                onChange={(value) => this.onChange(value, 'nightCount')}
              ></InputItem>
            </div>
          </div>
          <div className={styles.item} onClick={() => this.handleShowDatePicker('checkInDate')}>
            <div className={styles.des}>*Check-In Date</div>
            <div className={styles.value}>{ this.state.values.checkInDate || 'Select'}</div>
          </div>
          <div className={styles.item} onClick={() => this.handleShowDatePicker('checkOutDate')}>
            <div className={styles.des}>*Check-Out Date</div>
            <div className={styles.value}>{ this.state.values.checkOutDate || 'Select'}</div>
          </div>
          <div className={styles.item}>
            <div className={styles.des}>*Price（RMB）</div>
            <div className={styles.value}>
              <InputItem
                clear
                placeholder="click input"
                value={this.state.values.price}
                onChange={(value) => this.onChange(value, 'price')}
              ></InputItem>
            </div>
          </div>
          <div className={styles.item}>
            <div className={styles.des}>*Exceeded Standard</div>
            <div className={styles.value}>
              <Switch checked={this.state.values.exceedStd} onChange={(value) => this.onChange(value, 'exceedStd')}></Switch>
            </div>
          </div>
          <div className={styles.item}>
            <div className={styles.des}>Check Correct</div>
            <div className={styles.value}>
              <Switch checked={this.state.values.confirmFlag} onChange={(value) => this.onChange(value, 'confirmFlag')}></Switch>
            </div>
          </div>
          <div className={styles.item}>
            <div className={styles.des}>*Describe</div>
            <div className={styles.value}>
              <InputItem
                clear
                placeholder="click input"
                value={this.state.values.describe}
                onChange={(value) => this.onChange(value, 'describe')}
              ></InputItem>
            </div>
          </div>
          <div className={styles.item}>
            <div className={styles.des}>*Remark</div>
            <div className={styles.value}>
              <InputItem
                clear
                placeholder="click input"
                value={this.state.values.remark}
                onChange={(value) => this.onChange(value, 'remark')}
              ></InputItem>
            </div>
          </div>
        </div>
        </div>
        <div className={styles.footerBtns}>
          <div onClick={this.handleSuccess}>Save</div>
          <div onClick={this.handleCancel}>Cancel</div>
        </div>
      </div>
      </Modal>
    );
  }
}

export default {
  open(params){
    let div = document.createElement('div');
    document.body.appendChild(div);
    ReactDOM.render(React.createElement(addAccommodationInfoModal, params), div);
  }
};
